import os
import sys
from CalTorque import OpenCalTorqueDB
from mkplots import MkPlots
from PlotFit import PlotFit_mod
from average_torque_in import AverageIN
from average_torque_out import AverageOUT
from average_in_by_month import MonthlyAverages
from AverageCompareBABreak import CompareBeforeAfterBreak
from AverageCompareSources import CompareSources
from PlotMaxtvsDate import PlotMaxtvsDate
from PlotOffsetvsP0 import PlotOffsetvsP0
from plot_params import PlotParameters
import datetime

def Plot(dp = 'data/', bins = 400, max_d = 400):
    """ Create plots selected by user """
    
    # get which plots must be built
    plot = raw_input('Plot torque vs time and torque vs distance? (y/n)\n>')
    fit = raw_input('Plot Gaussian fit? (y/n)\n>')
    avgIn = raw_input('Plot average IN? (y/n) \n>')
    avgOut = raw_input('Plot average OUT? (y/n) \n>')
    month = raw_input('Plot monthly averages? (y/n) \n>')
    source = raw_input('Plot average for each source type? (y/n)\n>')
    telescope = raw_input('Plot average before and after the telescope break? (y/n)\n>')
    mtd = raw_input('Plot max torque versus date? (y/n)\n>')
    povp = raw_input('Plot offset versus initial position? (y/n)\n>')
    parms = raw_input('Plot gaussian parameters versus date? (y/n)\n>')
    
    # open database
    database = OpenCalTorqueDB("database.pkl")
    
    # get today's date for plot file names
    d = datetime.datetime.now()
    today = str(d.strftime('%Y%m%d'))
    
    # make plots
    if plot == 'y':
        print '\nPlotting torque vs time and torque vs distance'
        MkPlots(database)
    
    if fit == 'y':
        print '\nPlotting fits'
        for run in database:
            if run.NewParms() == None: continue
            print run
            PlotFit_mod(run)
    
    if avgIn == 'y':
        print '\nPlotting average IN'
        AverageIN(database, bins, max_d)
        
    if avgOut == 'y':
        print '\n Plotting average OUT'
        AverageOUT(database, bins, max_d)
    
    if month == 'y':
        print '\nPlotting average by month'
        MonthlyAverages(database, bins, max_d)

    if source == 'y':
        print '\nPlotting average by source type'
        CompareSources(database, bins, max_d)
    
    if telescope == 'y':
        print '\nPlotting average before and after telescope break'
        CompareBeforeAfterBreak(database, bins, max_d)
        
    if mtd == 'y':
        print '\nPlotting max torque versus date.'
        PlotMaxtvsDate(database, today)
    
    if povp == 'y':
        print '\nPlotting offset versus initial position.'
        PlotOffsetvsP0(database, today)
    
    if parms == 'y':
        print '\nPlotting gaussian parameters.'
        PlotParameters(database, today)

def main(argv = sys.argv):
    Plot()

if __name__ == '__main__':
    main(sys.argv)